package com.ai5suoai.ams.web.vo;

import com.ai5suoai.ams.entity.Authority;
import com.ai5suoai.ams.entity.User;
import com.fasterxml.jackson.annotation.JsonIgnore;

import org.hibernate.validator.constraints.Email;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * @author chenzhenjia
 * @since 2016/3/18
 */
public class UserVo implements Serializable, ToEntity<User> {
    private static final long serialVersionUID = 931203416844502526L;
    private String id;
    @Size(max = 50, message = "{name.not.null}")
    private String name;
    @JsonIgnore
    @NotNull(message = "{password.not.null}")
    @Size(min = 4, max = 50, message = "{password.size}")
    private String password;
    @NotNull(message = "{confirm.not.null}")
    @Size(min = 4, max = 50, message = "{confirm.size}")
    private String confirm;
    @NotNull(message = "{username.not.null}")
    @Size(min = 4, max = 50, message = "{username.size}")
    private String username;
    @Email(message = "{email}")
    @NotNull(message = "{email.not.null}")
    @Size(max = 50, message = "{email.size}")
    private String email;
    private boolean activated = false;
    private Set<Authority> authorities = new HashSet<Authority>();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isActivated() {
        return activated;
    }

    public void setActivated(boolean activated) {
        this.activated = activated;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirm() {
        return confirm;
    }

    public void setConfirm(String confirm) {
        this.confirm = confirm;
    }

    public Set<Authority> getAuthorities() {
        return authorities;
    }

    public void setAuthorities(Set<Authority> authorities) {
        this.authorities = authorities;
    }

    @Override
    public String toString() {
        return "UserVo{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", confirm='" + confirm + '\'' +
                ", username='" + username + '\'' +
                ", email='" + email + '\'' +
                ", activated=" + activated +
                ", authorities=" + authorities +
                '}';
    }

    @Override
    public User toEntity() {
        User user = new User();
        user.setId(getId());
        user.setName(getName());
        user.setPassword(getPassword());
        user.setUsername(getUsername());
        user.setEmail(getEmail() != null ? getEmail().toLowerCase() : null);
        user.setActivated(isActivated());
        user.setAuthorities(getAuthorities());
        return user;
    }
}
